part of pp_net;

class PPExceptionL10n {
  static String connectionError = "网络异常，请稍后再试";
  static String connectionTimeout = "连接超时";
  static String sendTimeout = "请求超时";
  static String receiveTimeout = "响应超时";
  static String requestSyntaxError = "请求语法错误";
  static String noPermission = "没有权限";
  static String serverRefusesExecute = "服务器拒绝执行";
  static String unableConnectServer = "无法连接服务器";
  static String requestMethodProhibited = "请求方法被禁止";
  static String serverInternalError = "服务器内部错误";
  static String invalidRequest = "无效的请求";
  static String serverOver = "服务器挂了";
  static String unsupportHttp = "不支持HTTP协议请求";
  static String unknowError1 = "未知错误 1";
  static String unknowError2 = "未知错误 2";
  static String unknowError3 = "未知错误 3";
}

/// 自定义异常
class PPException implements Exception {
  static int emptyCode = 1011;
  final String _message;
  final int _code;

  String get message => _message;
  int get code => _code;

  PPException(
    this._code,
    this._message,
  );

  @override
  String toString() {
    return "code = $_code , message = $_message";
  }

  String getMessage() {
    return _message;
  }

  factory PPException.create(DioException error) {
    switch (error.type) {
      case DioExceptionType.connectionError:
        {
          return BadRequestException(-1, PPExceptionL10n.connectionError);
        }
      case DioExceptionType.connectionTimeout:
        {
          return BadRequestException(-1, PPExceptionL10n.connectionTimeout);
        }
      case DioExceptionType.sendTimeout:
        {
          return BadRequestException(-1, PPExceptionL10n.sendTimeout);
        }
      case DioExceptionType.receiveTimeout:
        {
          return BadRequestException(-1, PPExceptionL10n.receiveTimeout);
        }
      case DioExceptionType.badResponse:
        {
          try {
            int? errCode = error.response?.statusCode;
            switch (errCode) {
              case 400:
                {
                  return BadRequestException(
                      errCode!, PPExceptionL10n.requestSyntaxError);
                }
              case 401:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.noPermission);
                }
              case 403:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.serverRefusesExecute);
                }
              case 404:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.unableConnectServer);
                }
              case 405:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.requestMethodProhibited);
                }
              case 500:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.serverInternalError);
                }
              case 502:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.invalidRequest);
                }
              case 503:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.serverOver);
                }
              case 505:
                {
                  return UnauthorisedException(
                      errCode!, PPExceptionL10n.unsupportHttp);
                }
              default:
                {
                  return PPException(
                      errCode!, error.message ?? PPExceptionL10n.unknowError1);
                }
            }
          } on Exception catch (e) {
            return PPException(-1, "${PPExceptionL10n.unknowError2} $e");
          }
        }
      default:
        {
          // PPLog.d(
          //     "[PPException], 错误 = $error, code=${error.type}, msg=${error.message}");
          var message = "";
          if (error.message != null) {
            message = "${error.message}";
          } else if (error.error != null) {
            message = "${error.error}";
          } else {
            message = PPExceptionL10n.unknowError3;
          }
          return PPException(-1, message);
        }
    }
  }
}

/// 请求错误
class BadRequestException extends PPException {
  BadRequestException(int code, String message) : super(code, message);
}

/// 未认证异常
class UnauthorisedException extends PPException {
  UnauthorisedException(int code, String message) : super(code, message);
}
